//
//  CustomLoginView.m
//  WeCommunity
//
//  Created by mac on 2017/6/20.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "CustomLoginView.h"
#import "UITextField+L_TextField.h"

typedef NS_ENUM(NSInteger, LoginTextContent) {
    Login_UserName = 0,            //账号
    Login_PassWord = 1,            //密码
    
};

@interface CustomLoginView ()<UIGestureRecognizerDelegate,UIScrollViewDelegate,UITextFieldDelegate>

@property (nonatomic,strong) UILabel * titleLabel;

@property (nonatomic,strong) UIScrollView * scrollView;

@property (nonatomic,strong) UITextField * userNameField;
@property (nonatomic,strong) UITextField * passWordField;

@property (nonatomic,strong) UIButton * loginButton;
@property (nonatomic,strong) UIButton * registerButton;

@property (nonatomic,assign) LoginTextContent loginTextContent;

@property (nonatomic,strong) UIButton * userAgreementChooseBtn;
@property (nonatomic,strong) UIButton * userAgreementContentBtn;

@end

@implementation CustomLoginView

-(instancetype)initWithFrame:(CGRect)frame withType:(LoginCustomUIType)type
{
    if (self = [super initWithFrame:frame]) {
        
        self.customUIType = type;
        
        [self addSubview:self.scrollView];
        
        [self.scrollView addSubview:self.titleLabel];
        
        
        self.userNameField = [self getCustomTextFieldWithType:Login_UserName];
        [self.scrollView addSubview:self.userNameField];
        
        self.passWordField = [self getCustomTextFieldWithType:Login_PassWord];
        [self.scrollView addSubview:self.passWordField];
        
        
        [self.scrollView addSubview:self.loginButton];
        
        [self.scrollView addSubview:self.registerButton];
        
        
        //RAC
        @weakify(self);
        [self.userNameField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
            @strongify(self);
            [self verifyTetxFieldContent];
        }];
        
        [self.passWordField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
            @strongify(self);
            [self verifyTetxFieldContent];
        }];
        
        
        if (self.customUIType == Type_Register){
            [self.scrollView addSubview:self.userAgreementContentBtn];
            [self.scrollView addSubview:self.userAgreementChooseBtn];
            
        }
    }
    return self;
}

#pragma mark - get
-(NSString *)username
{
    _username = self.userNameField.text;
    return _username;
}
-(NSString *)password
{
    _password = self.passWordField.text;
    return _password;
}

-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, KScreen_Width, CGRectGetHeight(self.bounds))];
        _scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + 1);
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.delegate = self;
        _scrollView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenKeyBoard)];
        tapGes.delegate = self;
        [_scrollView addGestureRecognizer:tapGes];
    }
    return _scrollView;
}


-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, KScreen_Width - 20, 24)];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = PFM_FONT(22);
        if (self.customUIType == Type_Login) {
            _titleLabel.text = KNSLocal(@"loginTitle");
        }else if (self.customUIType == Type_Register){
            _titleLabel.text = KNSLocal(@"regisTitle");
        }
        
    }
    return _titleLabel;
}

-(UITextField *)getCustomTextFieldWithType:(LoginTextContent)type
{
    UITextField * textfield = [[UITextField alloc] init];
    textfield.textColor = [UIColor blackColor];
    textfield.tintColor = TINT_GREEN_COLOR;
    textfield.font = PFR_FONT(14);
    textfield.delegate = self;
    textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    
    // leftView
    CGSize  leftSize = CGSizeMake(KScreen_Width * 0.24, 45);
    UIColor * leftColor = [UIColor blackColor];

    if (type == Login_UserName) {
        if (self.customUIType == Type_Login) {
            textfield.placeholder = KNSLocal(@"userNamePlaceholder");
        }else if (self.customUIType == Type_Register){
            textfield.placeholder = KNSLocal(@"R_userNamePlaceholder");
            [textfield inputTextMaxLength:12];
        }
        
        textfield.frame = CGRectMake(20, 150, KScreen_Width - 40, 45);
        textfield.returnKeyType = UIReturnKeyNext;
        
        [textfield setTextFieldLeftView:leftSize
                                   font:PFR_FONT(14)
                              textColor:leftColor
                                   text:KNSLocal(@"account")];
    }else if (type == Login_PassWord){
        if (self.customUIType == Type_Login) {
            textfield.placeholder = KNSLocal(@"passWordPlaceholder");
        }else if (self.customUIType == Type_Register){
            textfield.placeholder = KNSLocal(@"R_passWordPlaceholder");
            [textfield inputTextMaxLength:15];
        }
        textfield.frame = CGRectMake(20, 195, KScreen_Width - 40, 45);
        textfield.returnKeyType = UIReturnKeyDone;
        textfield.secureTextEntry = YES;
        
        [textfield setTextFieldLeftView:leftSize
                                   font:PFR_FONT(14)
                              textColor:leftColor
                                   text:KNSLocal(@"password")];
    }
    
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, KScreen_Width - 20, 0.5)];
    lineView.backgroundColor = LINE_COLOR;
    [textfield addSubview:lineView];
    
    return textfield;
}

-(UIButton *)loginButton
{
    if (!_loginButton) {
        _loginButton = [[UIButton alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(self.passWordField.frame) + 30,KScreen_Width - 40 , 48)];
        
        if (self.customUIType == Type_Login) {
            [_loginButton setTitle:KNSLocal(@"login") forState:UIControlStateNormal];
        }else if (self.customUIType == Type_Register){
            [_loginButton setTitle:KNSLocal(@"register") forState:UIControlStateNormal];
        }

        _loginButton.titleLabel.font = PFM_FONT(16);
        [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_loginButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
        _loginButton.layer.cornerRadius = 6.0;
        _loginButton.layer.borderWidth = 1.0;
        _loginButton.layer.borderColor = [UIColor colorWithHex:0x93c893 alpha:1.0].CGColor;
        _loginButton.clipsToBounds = YES;
        _loginButton.tag = 10;
        [_loginButton addTarget:self action:@selector(processButton:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _loginButton;
}

-(UIButton *)registerButton
{
    if (!_registerButton) {
        _registerButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMidX(self.bounds) - 50, CGRectGetMaxY(self.loginButton.frame) + 20,100 , 20)];
        
        
        if (self.customUIType == Type_Login) {
            [_registerButton setTitle:KNSLocal(@"register") forState:UIControlStateNormal];
        }else if (self.customUIType == Type_Register){
            [_registerButton setTitle:KNSLocal(@"returnLogin") forState:UIControlStateNormal];
        }
        
        [_registerButton setTitleColor:[UIColor colorWithHex:0x5f6e94 alpha:1.0] forState:UIControlStateNormal];
        [_registerButton setTitleColor:[UIColor colorWithHex:0x5f6e94 alpha:0.5] forState:UIControlStateSelected];
        _registerButton.titleLabel.font = PFR_FONT(12);
        _registerButton.tag = 11;
        [_registerButton addTarget:self action:@selector(processButton:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _registerButton;
}

-(UIButton *)userAgreementContentBtn
{
    if (!_userAgreementContentBtn) {
        _userAgreementContentBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMidX(self.bounds) - 50, CGRectGetMaxY(self.registerButton.frame) + 20, 100, 32)];
        [_userAgreementContentBtn setTitle:KNSLocal(@"userAgreementTitle") forState:UIControlStateNormal];
        [_userAgreementContentBtn setTitleColor:TINT_GREEN_COLOR forState:UIControlStateNormal];
        _userAgreementContentBtn.titleLabel.font = PFR_FONT(12);
        [_userAgreementContentBtn  addTarget:self action:@selector(proceeUserAgreementContentBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _userAgreementContentBtn;
}
-(UIButton *)userAgreementChooseBtn
{
    if (!_userAgreementChooseBtn) {
        _userAgreementChooseBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.userAgreementContentBtn.frame) - 20, CGRectGetMinY(self.userAgreementContentBtn.frame) + 6, 20, 20)];
        [_userAgreementChooseBtn setImage:[UIImage imageNamed:@"agree"] forState:UIControlStateNormal];
        [_userAgreementChooseBtn setImage:[UIImage imageNamed:@"agree_sl"] forState:UIControlStateSelected];
        [_userAgreementChooseBtn  addTarget:self action:@selector(proceeUserAgreementChooseBtn:) forControlEvents:UIControlEventTouchUpInside];
        _userAgreementChooseBtn.selected = YES;
    }
    return _userAgreementChooseBtn;
}


-(void)proceeUserAgreementContentBtn:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(processUserAgreemnntBtn)]) {
        [self.delegate processUserAgreemnntBtn];
    }
}

-(void)proceeUserAgreementChooseBtn:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if (sender.selected) {
        [self.userAgreementContentBtn setTitleColor:TINT_GREEN_COLOR forState:UIControlStateNormal];
    }else{
        [self.userAgreementContentBtn setTitleColor:[UIColor colorWithHex:0xbfbfbf alpha:1.0] forState:UIControlStateNormal];
    }
    [self verifyTetxFieldContent];
}

-(void)setloginButtonEnabled:(BOOL)enabled
{
    if (enabled) {
        self.loginButton.enabled = YES;
        [self.loginButton setBackgroundColor:[UIColor colorWithHex:0x51aa38 alpha:1.0]];
    }else{
        self.loginButton.enabled = NO;
        [self.loginButton setBackgroundColor:[UIColor colorWithHex:0xa3dea3 alpha:1.0]];
    }
}

#pragma mark - textField Delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == self.userNameField) {
        [self.userNameField resignFirstResponder];
        [self.passWordField becomeFirstResponder];
    }else if (textField == self.passWordField){
        [self.passWordField resignFirstResponder];
    }
    return NO;
}


#pragma mark - 验证输入内容
-(void)verifyTetxFieldContent
{
    if (self.customUIType == Type_Login) {
        if (self.passWordField.text.length > 0 &&
            self.userNameField.text.length > 0) {
            [self setloginButtonEnabled:YES];
        }else{
            [self setloginButtonEnabled:NO];
        }
    }else if (self.customUIType == Type_Register){
        if (self.userNameField.text.length >= 6 &&
            self.userNameField.text.length <= 12 &&
            self.passWordField.text.length >= 6 &&
            self.passWordField.text.length <= 15 &&
            self.userAgreementChooseBtn.selected == YES) {
            [self setloginButtonEnabled:YES];
        }else{
            [self setloginButtonEnabled:NO];
        }
    }
}

#pragma mark - 解决手势响应冲突
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UIScrollView"]) {
        return YES;
    }
    return NO;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self hiddenKeyBoard];
}

#pragma mark - 收回键盘
-(void)hiddenKeyBoard
{
    [self.userNameField resignFirstResponder];
    [self.passWordField resignFirstResponder];
}
#pragma mark - 显示键盘
-(void)showKeyBoard
{
    [self.userNameField becomeFirstResponder];
}

#pragma mark - 点击按钮
-(void)processButton:(UIButton *)sender
{
    if (self.delegate) {
        [self.delegate LoginViewProcessBtn:sender.tag - 10];
    }
}

@end
